package com.g2room.myuall.grap.lx;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.g2room.driving.grap.GenericGrap;
import com.g2room.myuall.grap.ky.model.po.GrapErr;
import com.g2room.myuall.grap.lx.model.po.SaUniv;
import com.g2room.myuall.grap.lx.model.po.SaUnivArti;
import com.g2room.myuall.util.DateUtil;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class GrapLxCountyArti implements Runnable {
	private static Map<String, String> areasMap = new HashMap<String, String>();
	private static Map<String, String> newsMap = new HashMap<String, String>();
	static final String source = "留学网";
	static final String baseUrl = "http://www.liuxue86.com";
	String code = "";
	String type ="";
	static {
		// 新闻资讯 http://www.liuxue86.com/news/yujin/
		newsMap.put("/news/yujin/","留学预警");
		newsMap.put("/news/liuxuexinwen/","留学新闻");
		newsMap.put("/news/shehui/","社会聚焦");
		newsMap.put("/news/zhengce/","留学政策");
		newsMap.put("/news/hangye/","行业动态");
		// 院校专业
		// http://www.liuxue86.com/yuanxiaozhuanye/zhuanyezixun/
		newsMap.put("/yuanxiaozhuanye/zhuanyezixun/","专业资讯");
		newsMap.put("/yuanxiaozhuanye/guojia/","国家优势");
		newsMap.put("/yuanxiaozhuanye/jiaoyutixi/", "教育体系");
		newsMap.put("/yuanxiaozhuanye/remenzhuanye/", "热门专业");
		// 文书写作
		// http://www.liuxue86.com/wenshuxiezuo/resume/
		newsMap.put("/wenshuxiezuo/resume/","个人简历");
		newsMap.put("/wenshuxiezuo/gerenchenshu/", "个人陈述");
		newsMap.put("/wenshuxiezuo/essay/", "MBA Essay");
		newsMap.put("/wenshuxiezuo/shenqingshu/", "申请书");
		newsMap.put("/wenshuxiezuo/tuijianxin/", "推荐信");
		newsMap.put("/wenshuxiezuo/dongjixin/", "动机信");
		newsMap.put("/wenshuxiezuo/shenqingcailiao/", "书面材料");
		newsMap.put("/wenshuxiezuo/taoci/", "套磁信");
		// 签证
		newsMap.put("http://visa.liuxue86.com/qianzhengzixun/","出国签证");
		newsMap.put("http://visa.liuxue86.com/huzhaobanli/","护照办理");
		newsMap.put("http://visa.liuxue86.com/banliliucheng/","签证办理流程");
		newsMap.put("http://visa.liuxue86.com/jiqiao/","签证技巧");
		
		// 留学生活
		// http://www.liuxue86.com/live/chujingrujing/
		newsMap.put("/live/liuxuedaikuan/","留学贷款/理财/外汇");
		newsMap.put("/live/chujingrujing/","出境入境");
		newsMap.put("/live/liuxuejingyan/","日记&经验");
		newsMap.put("/live/liuxuejiuye/","海外打工");
		newsMap.put("/live/yishizhuxing/","衣食住行");
		newsMap.put("/live/xingqianzhunbei/","行前准备");
//		newsMap.put("1111","22222");
//		newsMap.put("1111","22222");
//		newsMap.put("1111","22222");
		
		
		areasMap.put("meiguo","美国");
		areasMap.put("aozhou","澳洲");
		areasMap.put("yingguo","英国");
		areasMap.put("jianada","加拿大");
		areasMap.put("hanguo","韩国");
		areasMap.put("riben","日本");
		areasMap.put("eluosi","俄罗斯");
		areasMap.put("faguo","法国");
		areasMap.put("xinxilan","新西兰");
		areasMap.put("wukelan","乌克兰");
		areasMap.put("ruidian","瑞典");
		areasMap.put("aierlan","爱尔兰");
		areasMap.put("xinjiapo","新加坡");
		areasMap.put("helan","荷兰");
		areasMap.put("malaixiya","马来西亚");
		areasMap.put("nuowei","挪威");
		areasMap.put("deguo","德国");
		areasMap.put("danmai","丹麦");
		areasMap.put("fenlan","芬兰");
		areasMap.put("yidali","意大利");
		areasMap.put("ruishi","瑞士");
		areasMap.put("xibanya","西班牙");
		areasMap.put("aodili","奥地利");
	}

	//http://hanguo.liuxue86.com/school/
	GrapLxCountyArti() {}
	GrapLxCountyArti(String code, String type) {
		this.code = code;
		this.type = type;
	}
	public static void main(String[] args) throws Exception{
		// TODO  
		//  http://www.liuxue86.com/exam/

		//费用 http://www.liuxue86.com/feiyong/
		//申请条件  http://www.liuxue86.com/tiaojian/
		// 奖学金  http://www.liuxue86.com/jiangxuejin/
		
		
//		// 大学排名
//		// http://www.liuxue86.com/yuanxiaopaiming/
//		GrapLxCountyArti glca = new  GrapLxCountyArti();
//		for(String code : newsMap.keySet()) {
//			glca.getPages(code, newsMap.get(code));
//		}
		
		for(String code : newsMap.keySet()) {
			GrapLxCountyArti glca = new  GrapLxCountyArti(code, newsMap.get(code));
			Thread thread = new Thread(glca);
			thread.start();
		}
		
		
	}
	@Override
	public void run() {
		getPages(code, type);
	}
	/**
	 * 获取分页列表
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getPages(String code, String type) {
		System.out.println(code + "=" + type);
		Document doc = null;
		String url = (code.startsWith("http")) ? code : baseUrl + code;
		try {
			doc = GenericGrap.getDocByIp(url);
			if(doc == null) return;
			List<String> listPages = new ArrayList<String>();
			listPages.add(url);
			
			Element divEl = doc.getElementById("pages");
			if(null != divEl) {
				Elements bEls = divEl.getElementsByTag("a");
				if(null != bEls && !bEls.isEmpty() && bEls.size()>2) {
					// /yuanxiaozhuanye/guojia/10.html
					int pn = Integer.valueOf(bEls.get(bEls.size()-2).text());
					int page = pn / 50;
					for(int i =2; i<= pn; i++) {
						listPages.add(url + i + ".html");
					}
				}
			}
			
			for(String listUrl : listPages) {
				getList(listUrl, code, type);
			}
		} catch (Exception e) {
			System.out.println(url);
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("01");
			ge.setContent(type);
			ge.setDescription("获取Arti  Page错误："+e.getMessage());
			insert2ErrDb(ge);
		}
	}
	
	public void getList(String url, String code, String type) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
			if(doc == null) return;
			List<String> detailUrls = new ArrayList<String>();
			// 获取首页的置顶两条记录
			Elements els;
			try {
				els = doc.getElementsByAttributeValue("class", "list_nr_CC");
				if(null != els && !els.isEmpty() && els.size() > 1) {
					Elements aels = els.get(0).getElementsByTag("a");
					if(null != aels && !aels.isEmpty()) {
						detailUrls.add(aels.first().attr("href"));
					}
					aels = els.get(1).getElementsByTag("a");
					if(null != aels && !aels.isEmpty()) {
						detailUrls.add(aels.first().attr("href"));
					}
				}
			} catch(Exception e) {
				e.printStackTrace();
			}
			// 获取列表数据
			els = doc.getElementsByAttributeValue("class", "txt");
			if(null != els && !els.isEmpty()) {
				Elements liels = els.get(0).getElementsByTag("li");
				for(Element liEl : liels) {
					Elements aels = liEl.select("a");
					if(null != aels && !aels.isEmpty()) {
						if(aels.size() > 1) {
							detailUrls.add(aels.get(1).attr("href"));
						} else {
							detailUrls.add(aels.first().attr("href"));
						}
					}
				}
			}

			
			for(String listUrl : detailUrls) {
				getDetail(listUrl, code, type);
			}
		} catch(Exception e) {
			System.out.println(url);
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("02");
			ge.setContent(code);
			ge.setDescription(type + "_获取Arti  List错误："+e.getMessage());
			insert2ErrDb(ge);
		}
	}
	
	public void getDetail(String url, String code, String type) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
			if(doc == null) return;
			List<String> detailUrls = new ArrayList<String>();
			
			SaUnivArti sua = new SaUnivArti();
			sua.setTypeName(type);
			sua.setSource(source);
			sua.setSourceUrl(url);
			Elements els = doc.getElementsByAttributeValue("class", "nav_logo clearfix");
			if(null != els && !els.isEmpty()) {
				sua = getArti(els, sua, doc);
			} else {
				sua = getArti(sua, doc);
			}
			insert2ArtiDb(sua);
		} catch(Exception e) {
			if(null != e.getMessage() && e.getMessage().contains("404")) return;
			System.out.println(url);
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("04");
			ge.setContent(code);
			ge.setDescription(type + "_获取Arti  Detail错误："+e.getMessage());
			insert2ErrDb(ge);
		}
	}
	

	private SaUnivArti getArti(Elements els, SaUnivArti sua, Document doc) {
		Elements aels = els.get(0).getElementsByTag("a");
		if(null != aels) {
			String href = "";
			String countyCode = "";
			String county = "";
			for(Element el : aels) {
				href = el.attr("href");
				if(href.startsWith("http")) {
					// http://meiguo.liuxue86.com/news/liuxuexinwen/
					countyCode = href.substring(0, href.indexOf(".")).replace("http://", "");
					county = areasMap.get(countyCode);
					if(null != county) {
						sua.setCountyCode(countyCode);
						sua.setCountyName(county);
						break;
					}
				}
			}
		}

		// 获取内容 conter_main_one clearfix
		els = doc.getElementsByAttributeValue("class", "conter_main_one clearfix");
		if(null != els && !els.isEmpty()) {
			// title
			Elements subEls = els.get(0).getElementsByTag("h1");
			if(null != subEls&& !subEls.isEmpty()) {
				sua.setTitle(subEls.first().text());
			}
			// 时间
			subEls = els.get(0).getElementsByAttributeValue("class", "conter_main_one_nav clearfix");
			if(null != subEls&& !subEls.isEmpty()) {
				try {
					sua.setPublishTime(DateUtil.string2Date(subEls.first().getElementsByTag("p").first().text(), "yyyy-MM-dd"));
				} catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
		// 获取内容
		els = doc.getElementsByAttributeValue("class", "main_zhengw");
		if(null != els && !els.isEmpty()) {
			Elements childrenEls = els.get(0).children();
			boolean del = false;
			for(Element child : childrenEls) {
				if(child.text().contains("推荐阅读")) {
					del =true;
				} else if(!del && child.attr("class") != null && child.attr("class").contains("ye_780_four4")) {
					del =true;
				}
				if(del) child.remove();
			}
			sua.setContent(els.get(0).html());
		}
		return sua;
	}
	
	
	private SaUnivArti getArti(SaUnivArti sua, Document doc) {
		Element el = doc.getElementById("content_head");
		if(null != el) {
			// title
			Elements subEls = el.getElementsByTag("h1");
			if(null != subEls&& !subEls.isEmpty()) {
				sua.setTitle(subEls.first().text());
			}
			// 时间
			subEls = el.getElementsByTag("span");
			if(null != subEls&& !subEls.isEmpty()) {
				try {
					sua.setPublishTime(DateUtil.string2Date(subEls.first().text(), "yyyy-MM-dd"));
				} catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
		// 获取内容
		el = doc.getElementById("endtext");
		if(null != el) {
			Elements childrenEls = el.children();
			boolean del = false;
			for(Element child : childrenEls) {
				 if(!del && child.attr("class") != null && child.attr("class").contains("moreclass")) {
					del =true;
				}
				if(del) child.remove();
			}
			sua.setContent(el.html());
		}
		return sua;
	}

	public void insert2ArtiDb(SaUnivArti sua) throws Exception {
		PreparedStatement ps = null;
		try {
			// insert into sa_univ_arti(category_name,type_name,content,source_url,county_code,school_code,category_id,source,school_id,title,type_id,create_time,county_name,school_name,publish_time,) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,)
			String sql = "insert into sa_univ_arti(type_name,content,source_url,county_code,source,title,county_name,publish_time) values(?,?,?,?,?,?,?,?)";
			ps = DbUtil.getSaConn().prepareStatement(sql);
			// type_name,content,source_url,county_code,source,title,county_name,publish_time
			ps.setString(1, sua.getTypeName());
			ps.setString(2, sua.getContent());
			ps.setString(3, sua.getSourceUrl());
			ps.setString(4, sua.getCountyCode());
			ps.setString(5, sua.getSource());
			ps.setString(6, sua.getTitle());
			ps.setString(7, sua.getCountyName());
			ps.setDate(8, new java.sql.Date(sua.getPublishTime().getTime()));
			ps.execute();
			
		} catch(Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if(null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void insert2ErrDb(GrapErr ge) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getSaConn().prepareStatement("insert into grap_err(description,url,content,type) values(?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.execute();
			grapPs.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
